package com.lyy.shop;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.math.BigDecimal;
/**
 * @author Liangyaoyun
 */
public class GenForUser {
    /**
     * MySQL主机地址
     */
    private static final String MYSQL_HOST = "192.168.159.77:3306";
    /**
     * 数据库名
     */
    private static final String DB_NAME = "yunch_shop";
    /**
     * 数据库账号
     */
    private static final String MYSQL_USERNAME = "root";
    /**
     * 数据库密码
     */
    private static final String MYSQL_PASSWORD = "root";
    /**
     * 作者
     */
    private static final String AUTHOR = "Liangyaoyun";
    /**
     * 子模块名: 指定生成到哪个子项目中
     */
    private static final String MODULE_NAME = "shop-user";
    /**
     * 根包
     */
    private static final String BASE_PACKAGE = "com.lyy";
    /**
     * 数据库表名前缀
     */
    private static final String[] TABLE_PREFIX = {"ums_"};

    public static void main(String[] args) {
        // 将 BigDecimal 类型转为 Double 类型
        JdbcTypeMapping.registerMapping(BigDecimal.class, Double.class);
        new Generator(getDataSource(), getGlobalConfig()).generate();
    }

    /**
     * 获取MyBatisFlex生成器配置对象
     *
     * @return MyBatisFlex生成器配置对象
     */
    public static GlobalConfig getGlobalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();

        // 注释配置: 作者
        globalConfig.getJavadocConfig().setAuthor(AUTHOR);
        // 包配置: src目录，mapper配置文件目录，根包
        globalConfig.getPackageConfig()
                .setSourceDir(MODULE_NAME + "/src/main/java")
                .setMapperXmlPath(MODULE_NAME + "/src/main/resources/mapper")
                .setBasePackage(BASE_PACKAGE);
        // 策略配置: 表前缀，数据库名，忽略哪些表
        // 可调用 setGenerateTable() 来指定生成哪些表: 未配置时，默认生成指定数据库下的所有表
        // 可调用 setUnGenerateTable() 来指定排除哪些表: 未配置时，不排除任何表
        globalConfig.setGenerateTable(
                "ums_admin",
                "ums_admin_login_log",
                "ums_growth_change_history",
                "ums_integration_change_history",
                "ums_integration_consume_setting",
                "ums_member",
                "ums_member_level",
                "ums_member_login_log",
                "ums_member_receive_address",
                "ums_member_rule_setting",
                "ums_member_statistics_info",
                "ums_member_tag",
                "ums_member_task",
                "ums_menu",
                "ums_permission",
                "ums_resource",
                "ums_resource_category",
                "ums_role",
                "ums_role_menu_relation");
        globalConfig.getStrategyConfig()
                .setTablePrefix(TABLE_PREFIX)
                .setGenerateSchema(DB_NAME);
        // 生成实体类，辅助类，Mapper接口，Mapper配置，Service接口，Service实现类和Controller控制器
        globalConfig.enableEntity().setWithLombok(true);
        globalConfig.enableTableDef();
        globalConfig.enableMapper();
        globalConfig.enableMapperXml();
        globalConfig.enableService();
        globalConfig.enableServiceImpl()
                .setSuperClass(CacheableServiceImpl.class)
                .setCacheExample(true);
        globalConfig.enableController();
        // 返回MyBatisFlex生成器配置对象
        return globalConfig;
    }

    /***
     * 获取Hikari数据源对象
     *
     * @return Hikari数据源对象
     */
    public static DataSource getDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(MYSQL_USERNAME);
        dataSource.setPassword(MYSQL_PASSWORD);
        dataSource.setJdbcUrl("jdbc:mysql://" + MYSQL_HOST + "/" + DB_NAME
                + "?serverTimezone=Asia/Shanghai"
                + "&useUnicode=true&characterEncoding=utf-8");
        return dataSource;
    }
}
